import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $time 	= $Lazy.file(()=>import('@com/utils/time'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// 
const ControlModal 			= $Lazy.state(()=>import('./cpt/control-modal'))
const ControlModalC 		= $Lazy.state(()=>import('./cpt/control-modal-c'))
const ControlModalAlter		= $Lazy.state(()=>import('./cpt/control-modal-alter'))

// 账单类型
const PayType = [{name:'正常',value:'1'},{name:'冲抵',value:'2'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',			name: 'bill_phase_id',				 type: 'select', 	width:160,	loading:true, mode:'multiple' },
			{ label: '条码号',			name: 'spec_code' },
			{ label: '账单状态',			name: 'status',						 type: 'select', 		loading:true, idStr:'code' },
			{ label: '区域',			name: 'region_id',  				 type: 'select', 		loading:true },
			{ label: '接收标本时间',		names: ['start_at','end_at'], name:'jz-time', 	type:'date-range', format:1 },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label: '医院名称',			name: 'hosp_id',            		 type: 'select', 		loading:true },
			{ label: '业务员',			name: 'salesman_id', 				 type: 'select', 		loading:true },
			{ label: '病人姓名',			name: 'patient_name' },
			/* 帐单明细增加身份证查询 2021-08-18  --耿琰 */
			{ label: '身份证号',			name: 'card_no' },
			{ label: '项目名称',			name: 'kind_id', type: 'select' },
			{ label: '采集类型',			name: 'collect_type', 				 type: 'select', 		loading:true },
			{ label: '账单类型',			name: 'pay_type',					type: 'select', loading:true }
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr,show_del: 1 }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'bill_phase_id', await f.getCache({ api:'billPhaseSelect' } ) ) 
			// 账单状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:45300 } ) )
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) ) 
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 采集类型
			$fn.setRow(forms, 'collect_type', await f.collect_type )
			// 账单类型
			$fn.setRow(forms, 'pay_type', await f.bill_type )
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill/speclists')
	
	// 表单
	cols = [
		{ title: '账单编号',				dataIndex: 'order_sn',				width:200 },
		{ title: '条码号',				dataIndex: 'old_spec_code',			...$fn.table.spec_code },
		/* 账单明细报告添加字段显示第三方条码 2021-08-16  --耿琰 */
		{ title: '第三方条码', 			dataIndex: 'third_spec_code', 		width:120 },
		{ title: '接收标本日期',			dataIndex: 'sp_check_time',			...$fn.table.time },
		{ title: '开票单位',				dataIndex: 'organization_name',		width:150 },
		{ title: '开票单位编码',			dataIndex: 'organization_code',		width:150 },
		{ title: '姓名',				dataIndex: 'patient_name',			...$fn.table.name },
		/* 帐单明细增加身份证查询 2021-08-18  --耿琰 */
		{ title: '身份证号',				dataIndex: 'card_no', 				...$fn.table.idcard },
		{ title: '性别',				dataIndex: 'sex_name',				...$fn.table.sex },
		{ title: '年龄',				dataIndex: 'age',					...$fn.table.age },
		{ title: '所属医生',				dataIndex: 'doctor',				...$fn.table.name },
		{ title: '业务员名称',			dataIndex: 'salesman_user',			...$fn.table.name },
		{ title: '账期',				dataIndex: 'phase_str',				width:150 },
		{ title: '自然项目',				dataIndex: 'parent_kind_name',		width:250 },
		{ title: '采集类型',				dataIndex: 'collect_type_name',		...$fn.table.status },
		{ title: '账单类型',				dataIndex: 'pay_type',				...$fn.table.status, render: text => $fn.getState(PayType, text) },
		{ title: '账单创建日期', 		dataIndex: 'bill_created_at', 		...$fn.table.time },
		{ title: '医院名称',				dataIndex: 'hosp_name',				width:250 },
		{ title: '项目名称',				dataIndex: 'kind_name',				width:250 },
		{ title: '实验科室',				dataIndex: 'project_parent_name',	width:100 },
		{ title: '送检科室',				dataIndex: 'department_name',		width:150 },
		{ title: '标准价格',				dataIndex: 'sprice',				...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'price',					...$fn.table.num },
		// 新增初始结算价字段 20211018 耿琰
		{ title: '初始结算价',			dataIndex: 'initial_price', 			...$fn.table.num },
		{ title: '折扣率',				dataIndex: 'percent',				...$fn.table.num },
		{ title: '折扣金额',				dataIndex: 'perprice',				...$fn.table.num },
		{ title: '账单生成时间',			dataIndex: 'created_at',			...$fn.table.time},
		{ title: '账单状态',				dataIndex: 'status_name',			width:150 },
		{ title: '活动详情',				dataIndex: 'content', 				width:200 },	// 增加活动详情 2021-10-27  --马婷 
		{ title: '备注',					dataIndex: 'attribute_name',		width:150 },
		TableControls.controls.call(this,{ width:150 })
	]
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '操作详情', ()=> this.modal.open(rows) ),
			TableControls.click.call(this, '账单信息', ()=> this.modalc.open(rows) )
		]
	}
	// 操作按钮
	controls = () => {
		const { selectedRows, forms } = this.state
		return [
			Controls.click.call(this, '修改账期', ()=>this.controlModalAlteRef.open(selectedRows, forms[0].data), {isKeys:true} )							// 修改账期
		]
	}
	render(){
		return (
			<>
				<Page title='账单明细' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1-today' }) }
					{/* 表格 */}
					{ Table.call(this,{type:1,isInit: window.isInit}) }
				</Page>
				{/* 操作详情 */}
				<ControlModal onRef={ref=>this.modal=ref} />
				{/* 账单信息 */}
				<ControlModalC onRef={ref=>this.modalc=ref} />
				{/* 修改账期 */}
				<ControlModalAlter onRef={ref=>this.controlModalAlteRef=ref} onRefresh={()=>{
					this.fetch().then((data)=>{
						const { selectedKeys } = this.state
						let rows = []
						data.forEach((item)=>{
							selectedKeys.forEach((v)=>{
								if (v===item.uuid){rows.push(item)}
							})
						})
						this.setState({selectedRows:rows})
					})
				}}  />
			</>
		)
	}
}

export default Index